home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 1996 #1 / Amiga Plus CD - 1996 - No. 1.iso / pd / netz / xbtx_v1.1 / application.cpp next >
C/C++ Source or Header  |  1995-09-26  |  7KB  |  378 lines

  1. /*
  2. **    $Id: Application.cpp 1.4 1995/09/26 19:45:11 olsen Exp olsen $
  3. **
  4. **    :ts=4
  5. */
  6.  
  7. /*
  8.  * Copyright © 1995 by Olaf Barthel, All Rights Reserved
  9.  *
  10.  * Redistribution and use in source and binary forms, with or without
  11.  * modification, are permitted provided that the following conditions
  12.  * are met:
  13.  * 1. Redistributions of source code must retain the above copyright
  14.  *    notice, this list of conditions and the following disclaimer.
  15.  * 2. Redistributions in binary form must reproduce the above copyright
  16.  *    notice, this list of conditions and the following disclaimer in the
  17.  *    documentation and/or other materials provided with the distribution.
  18.  *
  19.  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
  20.  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
  22.  * EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24.  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  25.  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  26.  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  27.  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  28.  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29.  *
  30.  * This software has not been validated by the ``Bundesamt fuer Zulassungen in
  31.  * der Telekommunikation'' of the ``Deutsche Bundepost Telekom'' and thus
  32.  * must not be used for accessing the BTX-Network of the Telekom in Germany.
  33.  *
  34.  * Diese Software hat keine Zulassung durch das Bundesamt fuer Zulassungen in
  35.  * der Telekommunikation der Deutschen Bundespost Telekom und darf daher nicht
  36.  * am Netz der Deutschen Bundespost Telekom in Deutschland betrieben werden.
  37.  */
  38.  
  39. #include "Application.hpp"
  40.  
  41. #include <dos/dosextens.h>
  42.  
  43. #include <clib/exec_protos.h>
  44. #include <clib/dos_protos.h>
  45.  
  46. #ifdef __SASC
  47. #include <pragmas/exec_pragmas.h>
  48. #include <pragmas/dos_pragmas.h>
  49.  
  50. extern struct ExecBase        *SysBase;
  51. extern struct DosLibrary    *DOSBase;
  52. #endif    // _SASC
  53.  
  54. #include <stdio.h>
  55.  
  56. Application::Application()
  57. {
  58.     AppChannel    = NULL;
  59.     AppService    = NULL;
  60.     AppDisplay    = NULL;
  61.     AppModem    = NULL;
  62.  
  63.     JmpReady    = FALSE;
  64. }
  65.  
  66. Application::~Application()
  67. {
  68.     Close();
  69. }
  70.  
  71. VOID Application::Close(VOID)
  72. {
  73.     JmpReady = FALSE;
  74.  
  75.     if(AppDisplay)
  76.     {
  77.         delete AppDisplay;
  78.         AppDisplay = NULL;
  79.     }
  80.  
  81.     if(AppChannel)
  82.     {
  83.         delete AppChannel;
  84.         AppChannel = NULL;
  85.     }
  86.  
  87.     if(AppService)
  88.     {
  89.         delete AppService;
  90.         AppService = NULL;
  91.     }
  92.  
  93.     if(AppModem)
  94.     {
  95.         delete AppModem;
  96.         AppModem = NULL;
  97.     }
  98. }
  99.  
  100. STRPTR Application::Open(STRPTR PubScreen,int XScale,int YScale,BOOL TextOnly,BOOL Direct,CONST STRPTR Channel,ULONG Unit,ULONG Baud,BOOL RTS_CTS)
  101. {
  102.     IOChannel    *ThisChannel;
  103.     BTXDisplay    *ThisDisplay;
  104.     BOOL         IsSerial;
  105.  
  106.     if(ThisChannel = new IOFile)
  107.     {
  108.         IsSerial = FALSE;
  109.  
  110.         if(ThisChannel->Open(Channel))
  111.         {
  112.             delete ThisChannel;
  113.             ThisChannel = NULL;
  114.         }
  115.     }
  116.  
  117.     if(!ThisChannel)
  118.     {
  119.         if(ThisChannel = new IOSerial)
  120.         {
  121.             IsSerial = TRUE;
  122.  
  123.             if(ThisChannel->Open(Channel,Unit,Baud,RTS_CTS))
  124.             {
  125.                 delete ThisChannel;
  126.                 ThisChannel = NULL;
  127.             }
  128.         }
  129.     }
  130.  
  131.     if(ThisChannel)
  132.     {
  133.         if(TextOnly)
  134.             ThisDisplay = new TextDisplay;
  135.         else
  136.             ThisDisplay = new GfxDisplay;
  137.  
  138.         if(ThisDisplay)
  139.         {
  140.             if(ThisDisplay->Open(PubScreen,XScale,YScale,Direct))
  141.             {
  142.                 delete ThisDisplay;
  143.                 ThisDisplay = NULL;
  144.             }
  145.         }
  146.  
  147.         if(!ThisDisplay)
  148.         {
  149.             if(ThisDisplay = new TextDisplay)
  150.             {
  151.                 if(ThisDisplay->Open(PubScreen,XScale,YScale,Direct))
  152.                 {
  153.                     delete ThisDisplay;
  154.                     ThisDisplay = NULL;
  155.                 }
  156.             }
  157.         }
  158.  
  159.         if(ThisDisplay)
  160.         {
  161.             if(IsSerial)
  162.             {
  163.                 if(AppModem = new ModemService)
  164.                 {
  165.                     if(AppModem->Open(ThisChannel,ThisDisplay))
  166.                     {
  167.                         delete AppModem;
  168.                         AppModem = NULL;
  169.                     }
  170.                 }
  171.  
  172.                 if(!AppModem)
  173.                 {
  174.                     delete ThisChannel;
  175.                     ThisChannel = NULL;
  176.  
  177.                     delete ThisDisplay;
  178.                     ThisDisplay = NULL;
  179.  
  180.                     return("Error opening modem service.");
  181.                 }
  182.             }
  183.  
  184.             if(AppService = new BTXService)
  185.             {
  186.                 if(AppService->Open(ThisDisplay,this))
  187.                 {
  188.                     delete AppService;
  189.                     AppService = NULL;
  190.                 }
  191.             }
  192.  
  193.             if(AppService)
  194.             {
  195.                 AppChannel = ThisChannel;
  196.                 AppDisplay = ThisDisplay;
  197.  
  198.                 return(NULL);
  199.             }
  200.             else
  201.             {
  202.                 delete AppModem;
  203.                 AppModem = NULL;
  204.  
  205.                 delete AppChannel;
  206.                 AppChannel = NULL;
  207.  
  208.                 delete AppDisplay;
  209.                 AppDisplay = NULL;
  210.  
  211.                 return("Error opening BTX service.");
  212.             }
  213.         }
  214.         else
  215.         {
  216.             delete ThisChannel;
  217.  
  218.             return("Error opening display.");
  219.         }
  220.     }
  221.     else
  222.         return("Error opening serial channel.");
  223. }
  224.  
  225. LONG Application::DoEvent(VOID)
  226. {
  227.     ULONG Mask;
  228.  
  229.     if(!JmpReady)
  230.     {
  231.         if(setjmp(Home))
  232.             return(-1);
  233.         else
  234.             JmpReady = TRUE;
  235.     }
  236.  
  237.     Mask = AppChannel->WaitMask() | AppDisplay->WaitMask() | SIGBREAKF_CTRL_C;
  238.  
  239.     for(;;)
  240.     {
  241.         while(AppChannel->Waiting() || AppDisplay->Waiting())
  242.         {
  243.             if(AppChannel->Waiting())
  244.             {
  245.                 AppService->ProcessInput();
  246. /*
  247.  
  248.                 if(AppService->ProcessInput())
  249.                 {
  250.                     UBYTE Matrix[40 * 24];
  251.                     int x,y,i,j;
  252.  
  253.                     AppService->GetMatrix(Matrix,&x,&y);
  254.  
  255.                     Printf("\n\t+----------------------------------------+\n");
  256.  
  257.                     for(i = 0 ; i < 24 ; i++)
  258.                     {
  259.                         Printf("\t|");
  260.  
  261.                         if(i == y - 1)
  262.                         {
  263.                             for(j = 0 ; j < 40 ; j++)
  264.                             {
  265.                                 if(j == x - 1)
  266.                                     Printf("\33[32;43m%lc\33[31m\33[40m",Matrix[i * 40 + j]);
  267.                                 else
  268.                                     Printf("%lc",Matrix[i * 40 + j]);
  269.                             }
  270.                         }
  271.                         else
  272.                         {
  273.                             UBYTE Line[44];
  274.  
  275.                             CopyMem(&Matrix[i * 40],Line,40);
  276.  
  277.                             Line[40] = 0;
  278.  
  279.                             PutStr((STRPTR)Line);
  280.                         }
  281.  
  282.                         Printf("|\n");
  283.                     }
  284.  
  285.                     Printf("\t+----------------------------------------+\n");
  286.                 }
  287. */
  288.             }
  289.  
  290.             if(AppDisplay->Waiting())
  291.             {
  292.                 LONG Char = AppDisplay->GetChar();
  293.  
  294.                 if(Char >= 0)
  295.                 {
  296.                     if(Char == '\033')
  297.                         return(-1);
  298.                     else
  299.                         AppChannel->PutChar(Char);
  300.                 }
  301.             }
  302.         }
  303.  
  304.         if(Wait(Mask) & SIGBREAKF_CTRL_C)
  305.             return(-1);
  306.     }
  307. }
  308.  
  309. VOID Application::WaitForUserInput(VOID)
  310. {
  311.     ULONG Signals;
  312.  
  313.     AppDisplay->PutLine("[Hit any key to exit]");
  314.  
  315.     for(;;)
  316.     {
  317.         Signals = Wait(AppDisplay->WaitMask() | SIGBREAKF_CTRL_C);
  318.  
  319.         if(Signals & AppDisplay->WaitMask())
  320.         {
  321.             if(AppDisplay->Waiting())
  322.             {
  323.                 if(AppDisplay->GetChar() >= 0)
  324.                     break;
  325.             }
  326.         }
  327.  
  328.         if(Signals & SIGBREAKF_CTRL_C)
  329.             break;
  330.     }
  331. }
  332.  
  333. VOID Application::WaitEvent(VOID)
  334. {
  335.     Wait(AppChannel->WaitMask() | AppDisplay->WaitMask() | SIGBREAKF_CTRL_C);
  336. }
  337.  
  338. LONG Application::DispatchEvent(VOID)
  339. {
  340.     LONG Value;
  341.  
  342.     Value = DispatchDisplayEvent();
  343.  
  344.     if(Value == CHANNELERR_Nothing)
  345.         Value = (LONG)AppChannel->GetChar();
  346.  
  347.     return(Value);
  348. }
  349.  
  350. LONG Application::DispatchDisplayEvent(VOID)
  351. {
  352.     if(SetSignal(0,0) & SIGBREAKF_CTRL_C)
  353.         return(CHANNELERR_EOF);
  354.  
  355.     if(AppDisplay->Waiting())
  356.     {
  357.         LONG Char;
  358.  
  359.         Char = AppDisplay->GetChar();
  360.  
  361.         if(Char == '\033')
  362.             return(CHANNELERR_EOF);
  363.         else
  364.         {
  365.             if(Char >= 0)
  366.                 AppChannel->PutChar(Char);
  367.         }
  368.     }
  369.  
  370.     return(CHANNELERR_Nothing);
  371. }
  372.  
  373. VOID Application::EventExit(VOID)
  374. {
  375.     if(JmpReady)
  376.         longjmp(Home,1);
  377. }
  378.